package com.cm.goods.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cm.common.core.Result;
import com.cm.goods.entity.Type;
import com.cm.goods.entity.dto.TypeKeyWordDTO;
import com.cm.goods.mapper.TypeMapper;
import com.cm.goods.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @类 名:TypeController </br>
 * @描 述:<br>
 * @日 期:2020/9/23 18:20 </br>
 * @作 者:小兰同学</br>
 * @版 本：1.0.0
 * @since JDK 1.8
 */
@RestController
@RequestMapping("/type")
public class TypeController {
    @Autowired
    TypeMapper typeMapper;
    @Autowired
    TypeService typeService;

    @PostMapping("/insert")
    public Result insert (@RequestBody Type type){
        if (typeService.insert(type)) {
            return Result.success("类别添加成功");
        }
        return Result.fail(type.getName() + "已存在！");
    }

    @PostMapping("/delete")
    public Result delete(Long id) {
        if (typeService.delete(id)) {
            return Result.success("删除成功");
        }
        return Result.fail("删除失败");
    }

    @PostMapping("/update")
    public Result update(@RequestBody Type type) {
        if (typeService.update(type)) {
            return Result.success("修改成功");
        }
        return Result.fail("修改失败");
    }

    @PostMapping("/list")
    public Result getTypeList() {
        List<Type> typeList=typeService.getTypeList();
        return Result.success(typeList);
    }

    @PostMapping("/getById")
    public Result getById(Long id){
        Type type=typeMapper.selectById(id);
        return Result.success(type);
    }
}
